﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using System.Web.Mail;
using SFI.Web.Mail;
using System.Net.Mail;


namespace ShopOnline.Controllers
{
    public class SentMailController : Controller
    {
        private DAL_SentMail sendmailRepository;
        private ShopOnline.Models.ShopOnlineDBDataContext db;
        private DAL_Contact contactRepository;
        public SentMailController()
        {
            sendmailRepository = new DAL_SentMail();
            db = new Models.ShopOnlineDBDataContext();
            contactRepository = new DAL_Contact();
        }
        //
        // GET: /SentMail/

        public ActionResult AdminListSendMail()
        {
            var list = sendmailRepository.getListSentMail();
            return View(list);
        }

        //
        // GET: /SentMail/Details/5

        public ActionResult Details(int id)
        {
            var sendmail = sendmailRepository.getSentMaileById(id);
            return View(sendmail);
        }

        //
        // GET: /SentMail/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /SentMail/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                ShopOnline.Models.SentMail senmail = new Models.SentMail();
                senmail.address = collection["address"];
                senmail.body = collection["body"];
                senmail.dateSent = DateTime.Now;
                senmail.email = collection["email"];
                senmail.name = collection["name"];
                senmail.phone = collection["phone"];
                senmail.title = collection["title"]; ;
                var show = collection["show"];
                if (show=="false")
                {
                    senmail.show = false;
                }
                else
                {
                    senmail.show = true;
                }
                sendmailRepository.CreateSentMail(senmail);
                return RedirectToAction("AdminListSendMail");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /SentMail/Edit/5
 
        public ActionResult Edit(int id)
        {
            var sendmail = sendmailRepository.getSentMaileById(id);
            return View(sendmail);
        }

        //
        // POST: /SentMail/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                ShopOnline.Models.SentMail senmail = sendmailRepository.getSentMaileById(id);
                senmail.address = collection["address"];
                senmail.body = collection["body"];
                senmail.dateSent = DateTime.Now;
                senmail.email = collection["email"];
                senmail.name = collection["name"];
                senmail.phone = collection["phone"];
                senmail.title = collection["title"]; ;
                var show = collection["show"];
                if (show == "false")
                {
                    senmail.show = false;
                }
                else
                {
                    senmail.show = true;
                }
                sendmailRepository.EditSentMail(senmail);
                return RedirectToAction("AdminListSendMail");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SentMail/Delete/5
 
        public ActionResult Delete(int id)
        {
            var senmail = sendmailRepository.getSentMaileById(id);
            return View(senmail);
        }

        //
        // POST: /SentMail/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var senmail = sendmailRepository.getSentMaileById(id);
                sendmailRepository.DeleteSentMail(senmail);
                return RedirectToAction("AdminListSendMail");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult contactus()
        {
            var contact = contactRepository.getContact();
            ViewData["contact"] = contact;
            return View();
        }

        //
        // POST: /SentMail/Create

        [HttpPost]
        public ActionResult contactus(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                ShopOnline.Models.SentMail senmail = new Models.SentMail();
                senmail.address = collection["address"];
                senmail.body = collection["body"];
                senmail.dateSent = DateTime.Now;
                senmail.email = collection["email"];
                senmail.name = collection["name"];
                senmail.phone = collection["phone"];
                senmail.title = collection["title"]; 
                var show = collection["show"];
                senmail.show = true;
                sendmailRepository.CreateSentMail(senmail);
                #region sendMail
                 //SendMail("senmail ", "tienduydh219@gmail.com", "", senmail.name , senmail.name , senmail.address,
                 //                senmail.email, senmail.phone, senmail.body);

                SmtpClient smtpClient = new SmtpClient();
                System.Net.Mail.MailMessage message = new  System.Net.Mail.MailMessage();

                try
                {
                    MailAddress fromAddress = new MailAddress(senmail.email, senmail.title);

                    // You can specify the host name or ipaddress of your server

                    // Default in IIS will be localhost 

                    smtpClient.Host = "localhost";

                    //Default port will be 25

                    smtpClient.Port = 25;

                    //From address will be given as a MailAddress Object

                    message.From = fromAddress;

                    // To address collection of MailAddress

                    message.To.Add("tienduydh219@gmail.com");
                    message.Subject = "Feedback";

                    // CC and BCC optional

                    // MailAddressCollection class is used to send the email to various users

                    // You can specify Address as new MailAddress("admin1@yoursite.com")

                    // message.CC.Add("admin1@yoursite.com");
                    //message.CC.Add("admin2@yoursite.com");

                    // You can specify Address directly as string

                    // message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
                    // message.Bcc.Add(new MailAddress("admin4@yoursite.com"));

                    //Body can be Html or text format

                    //Specify true if it  is html message

                    message.IsBodyHtml = false;

                    // Message body content

                    message.Body = senmail.body;

                    // Send SMTP mail

                    smtpClient.Send(message);

                }
                catch (Exception ex)
                {

                }
                #endregion
                var contact = contactRepository.getContact();
                ViewData["contact"] = contact;
                return View();
            }
            catch
            {
                var contact = contactRepository.getContact();
                ViewData["contact"] = contact;
                return View();
            }
        }

        #region Method send mail

        public void SendMail(string toMail, string username, string password, string name, string company, string address, string email, string tel, string contentMessage)
        {
            var content = "Tên : " + name + "\n";
            content += "Company : " + company + "\n";
            content += "Address : " + address + "\n";
            content += "E-mail : " + email + "\n";
            content += "ContentMessage : " + contentMessage + "\n";
            content += "Tel : " + tel + "\n";
            content += "Noi Dung : " + contentMessage + "\n";


            var emailSubject = "[SCLAWFIRM] - Thong Tin Lien He " + name;
            var receiver = toMail;
            MailSender.SendGmail(username, password, receiver, emailSubject, content, MailFormat.Text,
                                 string.Empty);
        }

        #endregion

    }
}
